﻿using System;

public class Vector3
{
    public float x;
    public float y;
    public float z;
    public Vector3(float x, float y, float z)
    {
        this.x = x;
        this.y = y;
        this.z = z;
    }
    public Vector3(Vector3 p1, Vector3 p2)
    {
        this.x = p2.x-p1.x;
        this.y = p2.y-p1.y;
        this.z = p2.z-p1.z;
    }
    public Vector3(Vector3 v)
    {
        this.x = v.x;
        this.y = v.y;
        this.z = v.z;
    }
    public float length()
    {
        return (float)(Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2) + Math.Pow(z, 2)));
    }
    public float lengthSquared()
    {
        return (float)(Math.Pow(x, 2) + Math.Pow(y, 2) + Math.Pow(z, 2));
    }
    public Vector3 normalize()
    {
        float n=this.length();
        if (n != 0)
        {
            this.x /= n;
            this.y /= n;
            this.z /= n;
        }
        return this;
    }
    public float angle(Vector3 v)
    {
        float l1=this.length();
        float l2=v.length();
        float cos;
        if (l1 != 0 && l2 != 0)
        {
            cos = this.dot(v) / (l1 * l2);
            Console.WriteLine("Cosinus kąta między zadanymi wektorami wynosi " + cos);
            return cos;
        }
        else
        {
            Console.WriteLine("Co najmniej jeden z wektorów jest zerowy");
            return 2;
        }
    }
    public override string ToString()
    {
        return "(" + x + ", " + y + ", " + z + ")";
    }
    public static Vector3 operator +(Vector3 v)
    {
        return new Vector3(v.x, v.y, v.z);
    }
    public static Vector3 operator -(Vector3 v)
    {
        return new Vector3(-v.x, -v.y, -v.z);
    }
    public static Vector3 operator *(float k, Vector3 v)
    {
        return new Vector3(k * v.x, k * v.y, k * v.z);
    }
    public static Vector3 operator *(Vector3 v, float k)
    {
        return new Vector3(k * v.x, k * v.y, k * v.z);
    }
    public static Vector3 operator /(Vector3 v, float k)
    {
        if(k!=0)
            return new Vector3(v.x / k, v.y / k, v.z / k);
        else
            throw new Exception("Division by 0!");
    }
    public static Vector3 operator +(Vector3 v1, Vector3 v2)
    {
        return new Vector3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
    }
    public static Vector3 operator -(Vector3 v1, Vector3 v2)
    {
        return new Vector3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
    }
    public static Vector3 operator /(Vector3 v1, Vector3 v2)
    {
        if(v2.x!=0 && v2.y!=0 && v2.z!=0)
            return new Vector3(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z);
        else
            throw new Exception("Division by 0!");
    }
    public static Vector3 operator *(Vector3 v1, Vector3 v2)
    {
        return new Vector3(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z);
    }
    /*public static bool operator ==(Vector3 v1, Vector3 v2)
    {
        return (v1.x == v2.x && v1.y == v2.y && v1.z == v2.z);
    }
    public static bool operator !=(Vector3 v1, Vector3 v2)
    {
        return (v1.x != v2.x || v1.y != v2.y || v1.z != v2.z);
    }*/
    public Vector3 cross(Vector3 v)
    {
        return new Vector3(this.y * v.z - this.z * v.y, this.z * v.x - this.x * v.z, this.x * v.y - this.y * v.x);
    }
    public float dot(Vector3 v)
    {
        return (this.x * v.x + this.y * v.y + this.z * v.z);
    }
}
